package com.promise.jokerdream.model.log

import androidx.room.*
import kotlinx.coroutines.flow.Flow

/**
 * 执行日志数据访问对象
 */
@Dao
interface ExecutionLogDao {
    
    /**
     * 插入日志
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertLog(log: ExecutionLog)
    
    /**
     * 批量插入日志
     */
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertLogs(logs: List<ExecutionLog>)
    
    /**
     * 获取所有日志（按时间倒序）
     */
    @Query("SELECT * FROM execution_log ORDER BY timestamp DESC")
    fun getAllLogs(): Flow<List<ExecutionLog>>
    
    /**
     * 获取指定用户的所有日志（按时间倒序）
     */
    @Query("SELECT * FROM execution_log WHERE userId = :userId ORDER BY timestamp DESC")
    fun getLogsByUser(userId: String): Flow<List<ExecutionLog>>
    
    /**
     * 分页获取日志
     */
    @Query("SELECT * FROM execution_log ORDER BY timestamp DESC LIMIT :limit OFFSET :offset")
    suspend fun getLogsPaged(limit: Int, offset: Int): List<ExecutionLog>
    
    /**
     * 分页获取指定用户的日志
     */
    @Query("SELECT * FROM execution_log WHERE userId = :userId ORDER BY timestamp DESC LIMIT :limit OFFSET :offset")
    suspend fun getUserLogsPaged(userId: String, limit: Int, offset: Int): List<ExecutionLog>
    
    /**
     * 按操作类型获取日志
     */
    @Query("SELECT * FROM execution_log WHERE operation = :operation ORDER BY timestamp DESC")
    fun getLogsByOperation(operation: String): Flow<List<ExecutionLog>>
    
    /**
     * 按用户和操作类型获取日志
     */
    @Query("SELECT * FROM execution_log WHERE userId = :userId AND operation = :operation ORDER BY timestamp DESC")
    fun getUserLogsByOperation(userId: String, operation: String): Flow<List<ExecutionLog>>
    
    /**
     * 按结果状态获取日志
     */
    @Query("SELECT * FROM execution_log WHERE result = :result ORDER BY timestamp DESC")
    fun getLogsByResult(result: Int): Flow<List<ExecutionLog>>
    
    /**
     * 按用户和结果状态获取日志
     */
    @Query("SELECT * FROM execution_log WHERE userId = :userId AND result = :result ORDER BY timestamp DESC")
    fun getUserLogsByResult(userId: String, result: Int): Flow<List<ExecutionLog>>
    
    /**
     * 获取最近N条日志
     */
    @Query("SELECT * FROM execution_log ORDER BY timestamp DESC LIMIT :count")
    suspend fun getRecentLogs(count: Int): List<ExecutionLog>
    
    /**
     * 获取指定用户的最近N条日志
     */
    @Query("SELECT * FROM execution_log WHERE userId = :userId ORDER BY timestamp DESC LIMIT :count")
    suspend fun getUserRecentLogs(userId: String, count: Int): List<ExecutionLog>
    
    /**
     * 按时间范围获取日志
     */
    @Query("SELECT * FROM execution_log WHERE timestamp BETWEEN :startTime AND :endTime ORDER BY timestamp DESC")
    fun getLogsByTimeRange(startTime: Long, endTime: Long): Flow<List<ExecutionLog>>
    
    /**
     * 按用户和时间范围获取日志
     */
    @Query("SELECT * FROM execution_log WHERE userId = :userId AND timestamp BETWEEN :startTime AND :endTime ORDER BY timestamp DESC")
    fun getUserLogsByTimeRange(userId: String, startTime: Long, endTime: Long): Flow<List<ExecutionLog>>
    
    /**
     * 获取日志总数
     */
    @Query("SELECT COUNT(*) FROM execution_log")
    suspend fun getLogCount(): Int
    
    /**
     * 获取指定用户的日志总数
     */
    @Query("SELECT COUNT(*) FROM execution_log WHERE userId = :userId")
    suspend fun getUserLogCount(userId: String): Int
    
    
    /**
     * 删除指定日志
     */
    @Query("DELETE FROM execution_log WHERE id = :logId")
    suspend fun deleteLog(logId: String)
    
    /**
     * 删除指定时间之前的日志
     */
    @Query("DELETE FROM execution_log WHERE timestamp < :beforeTime")
    suspend fun deleteLogsBefore(beforeTime: Long)
    
    /**
     * 删除指定用户指定时间之前的日志
     */
    @Query("DELETE FROM execution_log WHERE userId = :userId AND timestamp < :beforeTime")
    suspend fun deleteUserLogsBefore(userId: String, beforeTime: Long)
    
    /**
     * 删除指定操作类型的日志
     */
    @Query("DELETE FROM execution_log WHERE operation = :operation")
    suspend fun deleteLogsByOperation(operation: String)
    
    /**
     * 删除指定用户的指定操作类型日志
     */
    @Query("DELETE FROM execution_log WHERE userId = :userId AND operation = :operation")
    suspend fun deleteUserLogsByOperation(userId: String, operation: String)
    
    /**
     * 清空所有日志
     */
    @Query("DELETE FROM execution_log")
    suspend fun clearAllLogs()
    
    /**
     * 清空指定用户的所有日志
     */
    @Query("DELETE FROM execution_log WHERE userId = :userId")
    suspend fun clearUserLogs(userId: String)
    
}
